import { login,register } from '../services/login';
import { routerRedux } from 'dva/router';
import { logInfo } from '../services/indexpage';
import {
  notification,Icon
} from 'antd';
export default {

    namespace: 'login',
    state: {
      username:"",
      password:"",
      isvip:0,
      registervisible:false,
      isCheckd:true
    },
  
    subscriptions: {
      setup({ dispatch, history }) {  // eslint-disable-line
        // var storage=window.localStorage;
        // var username = storage.getItem("username");
        // var password = storage.getItem("password");
        // var authorization = storage.getItem("authorization");
        // if(authorization){
        //   dispatch({
        //     type:'fetch',
        //     payload:{
        //       usernameOrEmailAddress:username,
        //       password:password,
        //       authorization:authorization
        //     }
        //   })
        // }
      },
    },
  
    effects: {
      *fetch({ payload }, { call, put }) {  // eslint-disable-line
        const result = yield call(login, payload); 
        console.log(result) 
        // console.log(result.err) 
        // console.log(result.data.isLogin) 
        if(result.data){
          sessionStorage.setItem("token",result.data.token);
       
          var isvip = result.data.IsVip.data[0]
          yield put(routerRedux.push({pathname: "/indexpage"})); 
          yield put({
            type:'setState',
            payload:{
               isvip:isvip
            }
          })
          notification.open({
            message: '登录成功',
            description: '您已登录成功',
            icon: <Icon type="smile" style={{ color: 'green' }} />,
          });
        }
      
        if(result.err){
          notification.open({
            message: '登录失败',
            description: '用户名不存在或密码错误',
            icon: <Icon type="warning" style={{ color: 'red' }} />,
          });
        }
         
     } , 
        *register({ payload }, { call, put }) { 
          const result = yield call(register, payload); 
          console.log(result) 
          if(result.data.isreg){
            notification.open({
              message: '注册成功',
              description: '您已成功注册用户',
              icon: <Icon type="smile" style={{ color: 'green' }} />,
            });
            yield put({
              type:'setState',
              payload:{
                registervisible:false
              }
            })
          }else {
            notification.open({
              message: '注册失败',
              description: '用户名已存在',
              icon: <Icon type="smile" style={{ color: 'red' }} />,
            });
          }
         
        },
     
    
      },
    
  
    reducers: {
      setState(state, action) {
        return { ...state, ...action.payload };
      },
    },
  
  };
  